<template>
  <div>
    <el-dialog title="编辑部门" :visible.sync="buttontype" @close="fun(tname)">
      <div>
        <el-form ref="form" :model="form" label-width="80px">
          <!-- 角色名称 -->
          <el-form-item label="栏目名称">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <!-- 角色编码 -->
          <el-form-item label="栏目编码">
            <el-input v-model="form.code"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="upuser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { http, channel } from "../../api/api";
export default {
  props: ["type", "fun", "tname", "tablerow"],
  data() {
    return {
      buttontype: this.type, //模态框状态
      formtype: {
        //数据
        name: "", //栏目名称
        code: "", // 栏目编码
      },
      form: "",
    };
  },
  methods: {
    upuser() {
      //  修改角色, 传进去
      var type = true;
      for (var i in this.formtype) {
        if (this.form[i] === "") {
          type = false;
        }
      }
      if (type) {
        this.$http
          .post(
            http + channel,
            {
              id: this.form.id,
              name: this.form.name,
              code: this.form.code,
            },
            { emulateJSON: true }
          )
          .then(
            (data) => {
              console.log(data.data);
              if (data.data.msg == "成功") {
                this.buttontype = false;
                // for (var i in this.form) {
                //   this.form[i] = "";
                // }
                this.form = "";
                this.$message.success("编辑数据成功");
              } else {
                this.$message.error(data.data.message);
              }
            },
            (error) => {
              /*  if (error.data.message == "该用户已经注册") {
              this.$message.error("该用户已经注册");
            } else {
              window.location = "./login.html";
            } */
              this.$message.error(error.data.message);
            }
          );
      } else {
        this.$message.error("请补全信息");
      }
    },
  },
  watch: {
    type() {
      this.buttontype = this.type;
    },
    tablerow() {
      // 浅拷贝变深拷贝
      var json = {};
      for (var i in this.tablerow) {
        this.$set(json, i, this.tablerow[i].toString());
      }
      this.form = json;
      console.log(this.form);
    },
  },
};
</script>

<style>
</style>